import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_base_plugin/base_library/base/base_function.dart';
import 'package:flutter_base_plugin_example/config/k_color.dart';
import 'package:flutter_base_plugin_example/page/login/vm/login_view_model.dart';

import '../../config/sp_key.dart';
import '../../utils/common_utils.dart';
import '../../widgets/jd_button.dart';
import '../../widgets/jd_editText.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _loginPageState();
  }
}

class _loginPageState extends BaseState<LoginPage> {
  @override
  void buildCreate() {
    // TODO: implement buildCreate
  }

  @override
  Widget buildWidget(BuildContext context) {
    // TODO: implement buildWidget
    return ProviderWidget<LoginViewModel>(
        viewModel: LoginViewModel(),
        onModelReady: (model) {
          model.getImgCode();
        },
        builder: (context, viewModel, _) {
          return _BuildWidget(viewModel);
        });
  }

  Widget _BuildWidget(LoginViewModel viewModel) {
    var picPath = viewModel.data?.picPath;
    return Container(
      child: SingleChildScrollView(
        padding: EdgeInsets.only(left: 40.w, right: 40.w),
        child: Column(
          children: [
            SizedBox(
              height: 150.h,
            ),
            Container(
              height: 200.w,
              width: 200.w,
              child: Image.asset(
                CommonUtils.getImgPath("app_icon"),
                fit: BoxFit.fill,
              ),
            ),
            SizedBox(
              height: 100.h,
            ),
            JdEditText(
              hintText: "用户名/手机号",
              onChanged: (value) {
                viewModel.username = value;
              },
            ),
            SizedBox(
              height: 20.h,
            ),
            JdEditText(
              hintText: "密码",
              password: true,
              onChanged: (value) {
                viewModel.password = value;
              },
            ),
            SizedBox(
              height: 20.h,
            ),
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //   children: [
            //     SizedBox(
            //       width: 200,
            //       child: JdEditText(
            //         hintText: "验证码",
            //         onChanged: (value) {
            //           viewModel.code = value;
            //         },
            //       ),
            //     ),
            //     InkWell(
            //       onTap: () {
            //         viewModel.getImgCode();
            //       },
            //       child: Container(
            //           width: 100,
            //           height: 50,
            //           child: Center(
            //             child: picPath == null
            //                 ? const Text("正在获取···")
            //                 : Image.memory(
            //                     base64.decode(picPath.split(",")[1]),
            //                     gaplessPlayback: true, //防止重绘
            //                   ),
            //           )),
            //     )
            //   ],
            // ),
            SizedBox(
              height: 20.h,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                InkWell(
                  child: Text("忘记密码"),
                ),
                InkWell(
                  onTap: () {
                    // pushPage(RouterPath.RegisterFirst);
                    ToastUtils.show(SpUtils.getString(SpKey.spAppToken));
                  },
                  child: Text("新用户注册"),
                )
              ],
            ),
            SizedBox(
              height: 20.h,
            ),
            JdButton(
              text: "登陆",
              color: KColors.appBarColor,
              onClick: () {
                // doLogin();
                viewModel.onLogin(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}
